<?php

class Article {
	
	public $id;
	public $naslov;
	public $slika;
	public $tekst;
	public $datum;
	public $vazna;
	public $prioritet;
	
	// constructor
	
	public function __construct($i = 0) {
		$this->id = $i;
		if ($i != 0) {
			$n = $this->getfromdb($i);
			$this->naslov = $n['naslov'];
			$this->slika = $n['slika'];
			$this->tekst = $n['tekst'];
			$this->vazna = $n['vazna'];
			$this->prioritet = $n['prioritet'];
			$this->datum = date("d.m.Y. H:i", strtotime($n['datum']));
		}
	}
	
	// functions
	
	private function getfromdb($number) {
		$q = "SELECT id, naslov, slika, tekst, datum, vazna, prioritet FROM vest WHERE id = " . $number;
		$r = mysql_query("set names 'utf8'");
		$r = mysql_query($q);
		if (!mysql_num_rows($r)) throw new Exception("Ne postoji vest za zadati ID!");
		return (mysql_fetch_array($r));
	}
	
	public function getFacebookUrl() {
		$domen = "http://www.basketliganis.org";
		$url = "";
		$url .= $domen . "/article/" . $this->id . "/" . str_replace(" ", "-", $this->naslov);
		return $url;
	}


	// admin
	
	public function insertIntoDb() {
		$q = "INSERT INTO vest (naslov, slika, tekst, datum, vazna, prioritet) VALUES ('".$this->naslov."', '".$this->slika."', '".$this->tekst."', '".$this->datum."', '".$this->vazna."', '".$this->prioritet."')";
		$r = mysql_query("set names 'utf8'");
		if (mysql_query($q)) {
			echo "Nova vest je dodata u bazu podataka";
		}
	}

		
} // end of class Article


?>